import requests
from requests_html import HTMLSession
import json
import time
from apscheduler.schedulers.blocking import BlockingScheduler

session = HTMLSession()
qqGroupList = [541512188]


def get_date():
    with open("date.txt", 'r') as file:
        return int(file.read())


def set_date(date):
    with open('date.txt', 'w') as file:
        file.write(str(date))


def get_messages(r):
    dataB = r.html.find('#getTimelineService', first=True).text
    dataB = dataB[34:-11]
    dataB = json.loads(dataB)
    return dataB


def run():
    try:
        get_date()
    except Exception:
        set_date(0)
    r = session.get('https://3g.dxy.cn/newh5/view/pneumonia')
    messages = get_messages(r)
    latest = messages[0]
    if latest['pubDate'] != get_date():
        set_date(latest['pubDate'])
        message = f'''{latest['title']}
{latest['summary']}
{latest['sourceUrl']}'''

        for q in qqGroupList:
            r = requests.get('http://127.0.0.1:5700/send_group_msg',
                            params={'access_token': 'your-gdfhuighuidfg', 'group_id': q, 'message': message},
                            )
            print(r.status_code)
            r.close()
            print("success ")


def schedar_run():
    print("自动群发实时播报最新消息功能---已启动")
    schedar = BlockingScheduler()
    schedar.add_job(run, "interval", seconds=60, id="9999")
    schedar.start()
